;;; GNU Guix --- Functional package management for GNU
;;;
;;; Copyright © 2022 (unmatched parenthesis <paren@disroot.org>
;;;
;;; This file is not part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus packages hare)
  #:use-module (gnu packages base)
  #:use-module (gnu packages c)
  #:use-module (gnu packages man)
  #:use-module (guix build-system copy)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module (guix gexp)
  #:use-module (guix git-download)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module ((guixrus contributors) #:prefix contributor:))

;;; NOTE: This file is only for executable programs/utilities written in Hare!
;;; Libraries should always go into guixrus/packages/common/hare.scm.

(define-public harec
  (let ((commit "4deca1ea5755338a21445a38710261888e34c120")
        (revision "2"))
    (package
     (name "harec")
     (version (git-version "0.0.0" revision commit))
     (source
      (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://git.sr.ht/~sircmpwn/harec")
             (commit commit)))
       (sha256
        (base32 "1x7b717kcyyw9ifyfrvd69bxd1hz9qvfgcgsdp4r17r0aq9niw78"))))
     (build-system gnu-build-system)
     (arguments
      `(#:phases
        ,#~(modify-phases %standard-phases
             (replace 'configure
               (lambda* (#:key outputs #:allow-other-keys)
                 (let ((out (assoc-ref outputs "out")))
                   (setenv "AR" #$(ar-for-target))
                   (setenv "CC" #$(cc-for-target))
                   (setenv "LD" #$(ld-for-target))
                   (invoke "./configure" (string-append "--prefix=" out))))))))
     (inputs (list qbe))
     (supported-systems (list "x86_64-linux" "aarch64-linux" "riscv64-linux"))
     (home-page "https://harelang.org")
     (synopsis "Hare bootstrap compiler in C")
     (description "This package provides @code{harec}, the Hare language's bootstrap written
in C. Currently, the self-hosting @code{harec} rewrite is incomplete, so this is used as the
default compiler in the build driver.")
     (license license:gpl3)
     (properties `((maintainers . (,contributor:unmatched-paren)))))))

(define-public hare
  (let ((commit "250d9c838289d269ffbede749c097be33ce2c2e7")
        (revision "2"))
     (package
      (name "hare")
      (version (git-version "0.0.0" revision commit))
      (source
       (origin
        (method git-fetch)
        (uri (git-reference
              (url "https://git.sr.ht/~sircmpwn/hare")
              (commit commit)))
        (sha256
         (base32 "1zwgmynp4q09iqhhfywgpq76jqqmdg64qbs6sqb2hiwxr8288hzl"))))
      (build-system gnu-build-system)
      (arguments
       `(#:phases
         (modify-phases %standard-phases
           (add-after 'unpack 'patch-failing-tests
             (lambda _
               ;; These tests fail due to a NaN-related bug in QBE when used on
               ;; glibc.
               (substitute* "math/complex/+test.ha"
                 (("@test (fn (cos|cosh|exp)\\(\\) void =)" _ func) func))))
           (add-before 'configure 'set-cache
             (lambda _
               (setenv "HARECACHE" ".cache")))
           (replace 'configure
             (lambda* (#:key outputs #:allow-other-keys)
               (let ((out (assoc-ref outputs "out")))
                 (rename-file "config.example.mk" "config.mk")
                 (substitute* "config.mk"
                   (("PREFIX = /usr") (string-append "PREFIX=" out))
                   (("LOCALSRCDIR = /usr/local/src/hare")
                    (string-append "LOCALSRCDIR=" out "/src/hare")))))))))
      (inputs (list harec qbe))
      (native-inputs (list scdoc))
      (native-search-paths
       (list (search-path-specification
              (variable "HAREPATH")
              (files '("src/hare/stdlib" "src/hare/third-party")))))
      (home-page "https://harelang.org")
      (synopsis "Simple, powerful systems programming language")
      (description "Hare is a work-in-progress systems programming language that aims to improve on
C while retaining its core philosophy. Its principles are:
@itemize
@item Trust the programmer.
@item Provide tools the programmer may use when they don't trust themselves.
@item Prefer explicit behavior over implicit behavior.
@item A good program must be both correct and simple.
@end itemize
Users should prefer @code{hare-toolchain} over this package to allow it to work out of the box.")
      (license (list license:gpl3 ;; compiler and build driver
                     license:mpl2.0 ;; standard library
                     ))
      (properties `((maintainers . (,contributor:unmatched-paren)))))))

(define-public hare-toolchain
  (package
    (name "hare-toolchain")
    (version (package-version hare))
    (source #f)
    (build-system trivial-build-system)
    (arguments `(#:builder (begin (mkdir %output))))
    (propagated-inputs (list binutils hare harec qbe))
    (native-search-paths (package-native-search-paths hare))
    (home-page (package-home-page hare))
    (synopsis "Complete @code{hare} toolchain for Hare development")
    (description "This package provides a toolchain for the Hare language. It allows you to conveniently
install all the required packages (such as @code{binutils} and @code{qbe}) into your profile.")
    (license (package-license hare))
    (properties `((maintainers . (,contributor:unmatched-paren))))))
